home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / util / cdity / yak158src.lha / settings.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-08  |  11.6 KB  |  360 lines

  1.  /*
  2.  * Variables controlling Yak settings.
  3.  * Routines for initialisation at startup.
  4.  */
  5.  
  6. #include <exec/types.h>
  7. #include <dos/dos.h>
  8. #include <dos/dosextens.h>
  9. #include <libraries/commodities.h>
  10. #include <proto/dos.h>
  11. #include <proto/exec.h>
  12. #include <string.h>
  13.  
  14. #include "yak.h"
  15. #include "hotkey_types.h"
  16. #include "gui.h"
  17. #define CATCOMP_NUMBERS
  18. #include "locale/yak_locale_strings.h"
  19.  
  20. #ifdef _SAS
  21. extern struct DosLibrary *DOSBase;
  22. #endif
  23.  
  24. /* local prototypes */
  25. static __regargs BOOL FWriteLong(BPTR fh, LONG n);
  26. static __regargs BOOL FReadLong(BPTR fh, LONG *n);
  27. static __regargs BOOL FWriteString(BPTR fh, char *buf);
  28. static __regargs BOOL FReadString(BPTR fh, char *buf, LONG len);
  29. static void LoadHotKeys(void);
  30. static void SaveHotKeys(void);
  31.  
  32. #define DEF_AUTOPOINT_DELAY      2
  33. LONG    autopoint_delay = DEF_AUTOPOINT_DELAY;  /* used for autopoint */
  34.  
  35. #define DEF_VOLUME      48
  36. LONG    click_volume = DEF_VOLUME;              /* used for keyclick */
  37.  
  38. #define DEF_BLANKSECS   300
  39. LONG    blanksecs = DEF_BLANKSECS;
  40. LONG    blanktimeout;
  41. LONG    blankcount;                             /* countdown to blank-time */
  42.  
  43. #define DEF_MBLANKSECS  5
  44. LONG    mouseblank = MB_SPRITES;
  45. LONG    mblanksecs = DEF_MBLANKSECS;
  46. LONG    mblanktimeout;
  47. LONG    mblankcount;                    /* countdown to mouse-blank-time */
  48.  
  49.  
  50. LONG    qualifier = MMB_NOTHING ;
  51.  
  52. TOGGLEDATA toggles[] = {                /* -1 means UNUSED */
  53.         TRUE,   GDX_CTFCheck,           ROOT_WINDOW,
  54.         TRUE,   GDX_CTBCheck,           ROOT_WINDOW,
  55.         TRUE,   GDX_AutoCheck,          ROOT_WINDOW,
  56.         FALSE,  GDX_KeyActCheck,        ROOT_WINDOW,
  57.         TRUE,   GDX_ScrCycleCheck,      ROOT_WINDOW,
  58.         FALSE,  GDX_AutoPopCheck,       ROOT_WINDOW,
  59.         FALSE,  GDX_RMBActCheck,        ROOT_WINDOW,
  60.         FALSE,  -1,                     NO_WINDOW,
  61.         FALSE,  -1,                     NO_WINDOW,
  62.         FALSE,  GDX_WildStarCheck,      MISC_WINDOW,
  63.         TRUE,   GDX_ScrActCheck,        ROOT_WINDOW,
  64.                 FALSE,  GDX_NoClickCheck,       MISC_WINDOW,
  65.                 FALSE,  GDX_MMBActCheck,        ROOT_WINDOW,
  66.                 FALSE,  GDX_BlackBorderCheck,   MISC_WINDOW,
  67.             TRUE,   GDX_BlankMouseOnKey,    BLANK_WINDOW
  68. };
  69.  
  70. PATTERNDATA patterns[NUM_PATTERNS] = {
  71.         { "#?", NULL },                 /* autoactivation screens */
  72.         { "#?", NULL },                 /* click screens */
  73.         { "~(Workbench)", NULL },       /* autopop windows */
  74.         { "~(Workbench)", NULL }        /* click windows */
  75. };
  76.  
  77. /* 1.5 format
  78.  * Routines to load/save config file for Yak.
  79.  * Should handle config files in upward-compatible manner.
  80.  * File format:
  81.  *
  82.  *      LONG ID
  83.  *      LONG NUM_TOGGLES
  84.  *      BOOL toggles
  85.  *      LONG NUM_HOTKEYS                **REMOVED @ v1.5
  86.  *      STR hotkey1 '\n'                        :
  87.  *              :                               :
  88.  *      STR hotkeyN '\n'                        :
  89.  *      LONG NUM_PATTERNS
  90.  *      STR pattern1 '\n'
  91.  *              :
  92.  *      STR patternN '\n'
  93.  *      STR popcommand '\n'             **REMOVED @ v1.5
  94.  *      STR datefmt '\n'                **REMOVED @ v1.5
  95.  *      LONG click_volume, blanksecs
  96.  *      LONG mblanksecs                 **ADDED @ v1.3b
  97.  *      LONG mouseblank                 **ADDED @ v1.3e
  98.  */
  99.  
  100. #define CONFIG_ID       0x594b3135      /* YK15 */
  101.  
  102. /* write a LONG to a file (in binary format) - returns success*/
  103. static __regargs BOOL
  104. FWriteLong(BPTR fh, LONG n)
  105. {
  106.         return (BOOL)(FWrite(fh, (UBYTE *)&n, sizeof(LONG), 1) == 1);
  107. }
  108.  
  109. /* read a LONG to a file (in binary format) - returns success */
  110. static __regargs BOOL
  111. FReadLong(BPTR fh, LONG *n)
  112. {
  113.         return (BOOL)(FRead(fh, (UBYTE *)n, sizeof(LONG), 1) == 1);
  114. }
  115.  
  116. /* write a string to a file (in binary format) - returns success*/
  117. /* '\n' is appended */
  118. static __regargs BOOL
  119. FWriteString(BPTR fh, char *buf)
  120. {
  121.         FPuts(fh, buf);
  122.         FPutC(fh, '\n');
  123.         return (BOOL)(IoErr() == 0);
  124. }
  125.  
  126. /* read a string to a file (in binary format) - returns success */
  127. /* '\n' is stripped and buf null-terminated; assumes dest large enough */
  128. static __regargs BOOL
  129. FReadString(BPTR fh, char *buf, LONG len)
  130. {
  131.         FGets(fh, buf, len-1);
  132.         buf[strlen(buf)-1] = '\0';      /* '\n' --> '\0' */
  133.         return (BOOL)(IoErr() == 0);
  134. }
  135.  
  136. /* save current settings to config file */
  137. void
  138. SaveSettings()
  139. {
  140.         BPTR    fh;
  141.         UWORD   i;
  142.  
  143.         if (fh = Open(CONFIG_FILE, MODE_NEWFILE))
  144.         {
  145.                 FWriteLong(fh, CONFIG_ID);
  146.  
  147.                 /* toggles */
  148.                 FWriteLong(fh, NUM_TOGGLES);
  149.                 for (i = 0; i < NUM_TOGGLES; i++)
  150.                         FWrite(fh, (UBYTE *)&toggles[i].pos, sizeof(BOOL), 1);
  151.  
  152.                 /* patterns */
  153.                 FWriteLong(fh, NUM_PATTERNS);
  154.                 for (i = 0; i < NUM_PATTERNS; i++)
  155.                         FWriteString(fh, patterns[i].patstr);
  156.  
  157.                 /* miscellaneous */
  158.                 FWriteLong(fh, click_volume);
  159.                 FWriteLong(fh, blanksecs);
  160.                 FWriteLong(fh, mblanksecs);
  161.                 FWriteLong(fh, mouseblank);
  162.                 FWriteLong(fh, autopoint_delay);
  163.  
  164.                 Close(fh);
  165.         }
  166.  
  167.         SaveHotKeys();
  168. }
  169.  
  170.  
  171.  
  172. /* load current settings from file */
  173. void
  174. LoadSettings()
  175. {
  176.         BPTR    fh;
  177.         UWORD   i;
  178.         LONG    n;
  179.  
  180.         if (fh = Open(CONFIG_FILE, MODE_OLDFILE))
  181.         {
  182.                 FReadLong(fh, &n);
  183.                 if (n == CONFIG_ID)
  184.                 {
  185.                         /* toggles */
  186.                         FReadLong(fh, &n);
  187.                         for (i = 0; i < n; i++)
  188.                                 FRead(fh, (UBYTE *)&toggles[i].pos, sizeof(BOOL), 1);
  189.  
  190.                         /* patterns */
  191.                         FReadLong(fh, &n);
  192.                         for (i = 0; i < n; i++)
  193.                                 FReadString(fh, patterns[i].patstr, PATLEN+1);
  194.  
  195.                         /* miscellaneous */
  196.                         FReadLong(fh, &click_volume);
  197.                         FReadLong(fh, &blanksecs);
  198.                         if (!FReadLong(fh, &mblanksecs))        /* none there */
  199.                                 mblanksecs = DEF_MBLANKSECS;
  200.                         if (!FReadLong(fh, &mouseblank))        /* none there */
  201.                                 mouseblank = MB_SPRITES;
  202.                         if (!FReadLong(fh, &autopoint_delay))   /* none there */
  203.                                 autopoint_delay = DEF_AUTOPOINT_DELAY;
  204.                 }
  205.                 else PostError(getString(Invalid_config_file_ERR));
  206.                 Close(fh);
  207.         }
  208.         DeleteYakHotKeyList();          /* delete old keys */
  209.         LoadHotKeys();                  /* and load new ones */
  210.  
  211.         /* set-up patterns */
  212.         for (i = 0; i < NUM_PATTERNS; i++)
  213.                 InitPattern(NULL, i);
  214.  
  215.         if (wildstar)
  216.                 WILDSTARON;
  217.         else
  218.                 wildstar = ((struct RootNode *)(((struct DosLibrary *)DOSBase)->dl_Root))->rn_Flags & RNF_WILDSTAR;
  219.  
  220.                 
  221.                 if (noclick) SetClickDrive(noclick);
  222.  
  223.                 if (blackborder) ToggleBlackBorder(blackborder);
  224.  
  225.         blankcount = blanktimeout = 10*blanksecs;
  226.         mblankcount = mblanktimeout = 10*mblanksecs;
  227. }
  228.  
  229. /*
  230.  *      HOTKEY FILE FORMAT
  231.  *
  232.  *      LONG    ID      'YKK1'
  233.  *      LONG NUM_HOTKEYS
  234.  *      UWORD type1
  235.  *      UWORD opts1
  236.  *      STR hotkey1 '\n'
  237.  *      STR argstr1 '\n'
  238.  *              :
  239.  *      STR hotkeyN '\n'
  240.  */
  241. #define HOTKEY_ID 0x594B4B31    /* 'YKK1' */
  242.  
  243. static void
  244. SaveHotKeys()
  245. {
  246.         YakHotKey *yhk;
  247.         BPTR    fh;
  248.         UWORD   type, i;
  249.         
  250.         if (fh = Open(HOTKEY_FILE, MODE_NEWFILE))
  251.         {
  252.                 FWriteLong(fh, HOTKEY_ID);
  253.                 
  254.                 FWriteLong(fh, num_hkeys);
  255.                 
  256.                 for (type = 0; type < NUM_HOTKEY_TYPES; type++)
  257.                         for (yhk = (YakHotKey *)keylist(type)->lh_Head, i = 0;
  258.                                  i < numkeys(type);
  259.                                  i++, yhk = (YakHotKey *)yhk->yhk_Node.ln_Succ)
  260.                         {
  261.                                 FWrite(fh, (UBYTE *)&yhk->yhk_Type, sizeof(UWORD), 1);
  262.                                 FWrite(fh, (UBYTE *)&yhk->yhk_Options, sizeof(UWORD), 1);
  263.                                 FWriteString(fh, yhk->yhk_KeyDef);
  264.                                 FWriteString(fh, yhk->yhk_ArgStr ? yhk->yhk_ArgStr : "");
  265.                         }
  266.  
  267.                 Close(fh);
  268.         }
  269. }
  270.  
  271. static void
  272. LoadHotKeys()
  273. {
  274.         YakHotKey *yhk;
  275.         char    *buf;
  276.         UWORD   type, opts;
  277.         BPTR    fh;
  278.         LONG    n;
  279.         UWORD   i;
  280.  
  281.  
  282.         DEBUG_BEGIN("LoadHotKeys()");
  283.  
  284.         if (!(buf = AllocVec(512, 0L)))
  285.         {
  286.                 PostError(getString(No_memory_ERR));
  287.                 return;
  288.         }
  289.         
  290.         if (fh = Open(HOTKEY_FILE, MODE_OLDFILE))
  291.         {
  292.                 FReadLong(fh, &n);
  293.                 
  294.                 if (n == HOTKEY_ID)
  295.                 {
  296.                         FReadLong(fh, &n);
  297.  
  298.                         DEBUG_PRINTF("  n = %d\n",n);
  299.  
  300.                         for (i = 0; i < n; i++)
  301.                         {
  302.  
  303.                                 DEBUG_PRINTF("  i = %d\n",i);
  304.  
  305.                                 if (FRead(fh, (UBYTE *)&type, sizeof(UWORD), 1) && 
  306.                                         FRead(fh, (UBYTE *)&opts, sizeof(UWORD), 1))
  307.                                 {               
  308.  
  309.                                         DEBUG_PRINTF("  type = %d\n",type);
  310.                                         DEBUG_PRINTF("  opts = %d\n",opts);
  311.  
  312.                                         if ((type >= NUM_HOTKEY_TYPES) || (type < 0))
  313.                                         {
  314.                                                 /* ignore key definition */
  315.                                                 FReadString(fh, buf, 512);
  316.                                                 FReadString(fh, buf, 512);
  317.                                                 continue;
  318.                                         }
  319.                                         else
  320.                                         {
  321.                                                 if (yhk = NewYakHotKey(type))
  322.                                                 {
  323.                                                         yhk->yhk_Options = opts;
  324.                                                         FReadString(fh, buf, 512);
  325.  
  326.                                                         DEBUG_PRINTF("  buf = %s\n",buf);
  327.  
  328.                                                         if (ModifyYHKKeyDef(yhk, buf))
  329.                                                         {
  330.  
  331.                                                                 FReadString(fh, buf, 512);
  332.                                                                 if (!buf[0] || ModifyYHKArgStr(yhk, buf))
  333.                                                                 {
  334.                                                                         continue;
  335.                                                                 }
  336.                                                         }
  337.                                                 } /* end if (yhk = NewYakHotKey(type)) */
  338.  
  339.                                         } /* end else ((type >= NUM_HOTKEY_TYPES) || (type < 0)) */
  340.  
  341.                                 }/* end if FRead (opts, type) */
  342.         
  343.                         } /* end for */
  344.                 }
  345.                 else
  346.                 {
  347.                         PostError(getString(Error_reading_hotkey_file_ERR));
  348.  
  349.                 } /* end if (n == HOTKEY_ID) */
  350.         
  351.  
  352.                 DEBUG_PRINTF("num_hkeys = %d\n",num_hkeys);
  353.  
  354.                 Close(fh);
  355.  
  356.         } /* end if fh */
  357.  
  358.         FreeVec(buf);
  359. }
  360.